package com.jh3502.vivo.function.tododetail.reim;

import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.function.todo.TodoListBean;
import com.jh3502.vivo.utils.Constant;
import com.jh3502.vivo.view.MyRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author:harisucici on 2018/11/7 10:42
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class TodoReimActivity extends BaseActivity<TodoReimPresenter.TodoReimView, TodoReimPresenter> implements TodoReimPresenter.TodoReimView {

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.todo_detail_reim_name)
    TextView todoDetailReimName;
    @BindView(R.id.todo_detail_reim_point)
    ImageView todoDetailReimPoint;
    @BindView(R.id.todo_detail_reim_status)
    TextView todoDetailReimStatus;
    @BindView(R.id.todo_detail_reim_person)
    ImageView todoDetailReimPerson;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.todo_detail_reim_num)
    TextView todoDetailReimNum;
    @BindView(R.id.todo_detail_reim_date)
    TextView todoDetailReimDate;
    @BindView(R.id.todo_detail_reim_dep)
    TextView todoDetailReimDep;
    @BindView(R.id.todo_detail_reim_iv1)
    ImageView todoDetailReimIv1;
    @BindView(R.id.todo_detail_reim_msg)
    ConstraintLayout todoDetailReimMsg;
    @BindView(R.id.todo_detail_reim_iv2)
    ImageView todoDetailReimIv2;
    @BindView(R.id.todo_detail_reim_change)
    ConstraintLayout todoDetailReimChange;
    @BindView(R.id.todo_detail_approve_delete)
    TextView todoDetailApproveDelete;
    @BindView(R.id.todo_detail_reject)
    TextView todoDetailReject;
    @BindView(R.id.todo_detail_submit)
    TextView todoDetailSubmit;
    @BindView(R.id.lapprove)
    LinearLayout lapprove;
    @BindView(R.id.todo_detail_delete)
    TextView todoDetailDelete;
    @BindView(R.id.todo_detail_change)
    TextView todoDetailChange;
    @BindView(R.id.lstart)
    LinearLayout lstart;
    @BindView(R.id.todo_detail_reim_list)
    MyRecyclerView todoDetailReimList;
    @BindView(R.id.l1)
    LinearLayout l1;
    @BindView(R.id.include_todo_detail_reim_msg)
    View includeTodoDetailReimMsg;
    @BindView(R.id.todo_detail_reim_travel_num)
    TextView todoDetailReimTravelNum;
    @BindView(R.id.todo_detail_reim_audits)
    MyRecyclerView todoDetailReimAudits;
    @BindView(R.id.total)
    TextView total;
    @BindView(R.id.textView4)
    TextView textView4;
    @BindView(R.id.textView5)
    TextView textView5;
    @BindView(R.id.textView11)
    TextView textView11;
    @BindView(R.id.tv0)
    TextView tv0;
    @BindView(R.id.textView7)
    TextView textView7;
    @BindView(R.id.imageView6)
    ImageView imageView6;
    @BindView(R.id.tv1)
    TextView tv1;
    private TodoListBean todoListBean;
    private List<Map<String, Object>> bodyList = new ArrayList<>(2);

    @Override
    public TodoReimPresenter initPresenter() {
        return new TodoReimPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_todo_detail_reim;
    }

    @Override
    protected void initView() {

        toolbar.setTitle("");
        toolbarTitle.setText("报销");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mPresenter.setTodoReimView(this);

        Map<String, Object> body = new HashMap<>(2);
        body.put("id", getIntent().getExtras().getInt("id"));
        mPresenter.toGetExpenseById(body);

        Map<String, Object> body1 = new HashMap<>(2);
        body1.put("businessId", getIntent().getExtras().getInt("id"));
        mPresenter.toFindExpenseAudits(body1);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            default:
        }
        return true;
    }

    @Override
    protected void initData() {

        if (!TextUtils.isEmpty(getIntent().getExtras().getString("bean"))) {
            todoListBean = new Gson().fromJson(getIntent().getExtras().getString("bean"),
                    TodoListBean.class);
        }
    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {

    }

    @Override
    public void getExpenseByIdSuccess(TodoReimBean todoReimBean) {
        int source = getIntent().getIntExtra("source", -1);
        todoDetailReimName.setText(todoReimBean.getName() + "的报销审批");
        int state = todoReimBean.getState();
        int audstate  = getIntent().getIntExtra("audstate",-1);
        if(audstate!=-1){}

        if (state == 0) {
            if (audstate==1){
                Glide.with(this).load(R.drawable.point_green).into(todoDetailReimPoint);
                todoDetailReimStatus.setText("审批中");
                todoDetailReimStatus.setTextColor(Color.parseColor("#FF0EC053"));
                if (source == Constant.REIM_START) {
                    lstart.setVisibility(View.VISIBLE);
                    lapprove.setVisibility(View.GONE);
                } else {
                    lstart.setVisibility(View.GONE);
                    lapprove.setVisibility(View.VISIBLE);
                }
                lstart.setVisibility(View.VISIBLE);
                todoDetailApproveDelete.setVisibility(View.VISIBLE);
                todoDetailReject.setVisibility(View.GONE);
                todoDetailSubmit.setVisibility(View.GONE);
            }else{
                Glide.with(this).load(R.drawable.point_red).into(todoDetailReimPoint);
                todoDetailReimStatus.setText("待审批");
                todoDetailReimStatus.setTextColor(Color.parseColor("#FFF36C60"));
                if (source == Constant.REIM_START) {
                    lstart.setVisibility(View.VISIBLE);
                    lapprove.setVisibility(View.GONE);
                } else {
                    lstart.setVisibility(View.GONE);
                    lapprove.setVisibility(View.VISIBLE);
                }
                lstart.setVisibility(View.GONE);
                todoDetailApproveDelete.setVisibility(View.GONE);
                todoDetailReject.setVisibility(View.VISIBLE);
                todoDetailSubmit.setVisibility(View.VISIBLE);
            }

        } else if (state == 1) {
            Glide.with(this).load(R.drawable.point_green).into(todoDetailReimPoint);
            todoDetailReimStatus.setText("已批准");
            todoDetailReimStatus.setTextColor(Color.parseColor("#FF0EC053"));

            if (source == Constant.REIM_START) {
                lstart.setVisibility(View.VISIBLE);
                lapprove.setVisibility(View.GONE);
            } else {
                lstart.setVisibility(View.GONE);
                lapprove.setVisibility(View.VISIBLE);
            }
            lstart.setVisibility(View.VISIBLE);
            todoDetailApproveDelete.setVisibility(View.VISIBLE);
            todoDetailReject.setVisibility(View.GONE);
            todoDetailSubmit.setVisibility(View.GONE);

        } else if (state == 2) {
            Glide.with(this).load(R.drawable.point_blue).into(todoDetailReimPoint);
            todoDetailReimStatus.setText("审批被拒绝");
            todoDetailReimStatus.setTextColor(Color.parseColor("#FF3389FF"));

            if (source == Constant.REIM_START) {
                lstart.setVisibility(View.VISIBLE);
                lapprove.setVisibility(View.GONE);
            } else {
                lstart.setVisibility(View.GONE);
                lapprove.setVisibility(View.VISIBLE);
            }
            lstart.setVisibility(View.VISIBLE);
            todoDetailApproveDelete.setVisibility(View.VISIBLE);
            todoDetailReject.setVisibility(View.GONE);
            todoDetailSubmit.setVisibility(View.GONE);
        }
        total.setText(todoReimBean.getActMoney() + "元");
        todoDetailReimNum.setText(todoReimBean.getBusinessNo());
        todoDetailReimDate.setText(todoReimBean.getCreateDate());
        todoDetailReimDep.setText(todoReimBean.getName());
        todoDetailReimTravelNum.setText(todoReimBean.getExpense().getTravelNo() + "");
        todoDetailReimList.setNestedScrollingEnabled(false);
        TodoReimAdapter mAdapter = new TodoReimAdapter(this, todoReimBean.getExpense().getEdInfo());
        todoDetailReimList.setAdapter(mAdapter);
    }


    @Override
    public void findExpenseAuditsSuccess(List<TodoReimAuditsBean> list) {
        todoDetailReimAudits.setNestedScrollingEnabled(false);
        TodoReimAuditsAdapter mAdapter = new TodoReimAuditsAdapter(this, list);
        todoDetailReimAudits.setAdapter(mAdapter);
    }

    @Override
    public void agreeForAllSuccess() {
        showShortToast("操作成功");
        onBackPressed();
    }

    @Override
    public void deleteAuditSuccess() {
        showShortToast("删除成功");
        onBackPressed();
    }

    @Override
    public void deleteTaskSuccess() {
        showShortToast("删除成功");
        onBackPressed();
    }

    @OnClick(R.id.todo_detail_reim_msg)
    public void onTodoDetailReimMsgClicked() {
        if (includeTodoDetailReimMsg.getVisibility() == View.GONE) {
            todoDetailReimMsg.setBackgroundColor(Color.parseColor("#FFE2E7E9"));
            Glide.with(this).load(R.drawable.ic_expand_less_black_24dp).into(todoDetailReimIv1);
            includeTodoDetailReimMsg.setVisibility(View.VISIBLE);
        } else {
            todoDetailReimMsg.setBackgroundColor(Color.WHITE);
            Glide.with(this).load(R.drawable.ic_expand_more_black_24dp).into(todoDetailReimIv1);
            includeTodoDetailReimMsg.setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.todo_detail_reim_change)
    public void onTodoDetailReimChangeClicked() {
        if (todoDetailReimAudits.getVisibility() == View.GONE) {
            todoDetailReimChange.setBackgroundColor(Color.parseColor("#FFE2E7E9"));
            Glide.with(this).load(R.drawable.ic_expand_less_black_24dp).into(todoDetailReimIv2);
            todoDetailReimAudits.setVisibility(View.VISIBLE);
        } else {
            todoDetailReimChange.setBackgroundColor(Color.WHITE);
            Glide.with(this).load(R.drawable.ic_expand_more_black_24dp).into(todoDetailReimIv2);
            todoDetailReimAudits.setVisibility(View.GONE);
        }
    }

    private Map<String, Object> doApprove(int state, TodoListBean todoListBean) {
        Map<String, Object> body = new HashMap<>(2);
        body.put("userId", todoListBean.getUserId());
        body.put("type", todoListBean.getType());
        body.put("state", state);
        body.put("destination", todoListBean.getDestination());
        body.put("businessNo", todoListBean.getBusinessNo());
        body.put("id", todoListBean.getId());
        body.put("flowNo", todoListBean.getFlowNo());
        body.put("currentNodeNo", todoListBean.getCurrentNodeNo());
        return body;
    }

    @OnClick(R.id.todo_detail_approve_delete)
    public void onTodoDetailApproveDeleteClicked() {
        Map<String, Object> body = new HashMap<>(2);
        body.put("id", todoListBean.getAuditId());
        body.put("type", Constant.TYPE_REIM);
        mPresenter.toDeleteAudit(body);
    }

    @OnClick(R.id.todo_detail_reject)
    public void onTodoDetailRejectClicked() {
        bodyList.clear();
        bodyList.add(doApprove(2, todoListBean));
        mPresenter.toAgreeForAll(bodyList);
    }

    @OnClick(R.id.todo_detail_submit)
    public void onTodoDetailSubmitClicked() {
        bodyList.clear();
        bodyList.add(doApprove(1, todoListBean));
        mPresenter.toAgreeForAll(bodyList);
    }


    @OnClick(R.id.todo_detail_delete)
    public void onTodoDetailDeleteClicked() {
        Map<String, Object> body = new HashMap<>(2);
        body.put("id", getIntent().getExtras().getInt("id"));
        mPresenter.toDeleteTask(body);
    }
}
